<template>
    <el-dialog v-if="dialogVisible" :close-on-click-modal='false' title="产品列表" :visible.sync="dialogVisible" @close="resetForm()" width="400px">
        <el-form v-if="show" :model="addProdutForm" :hide-required-asterisk='true' ref="addProdutForm" :rules="addProdutRules" class="dialog_form">
            <el-form-item label="" prop="name">
                <div class="flexbox flexcenter">
                    <el-input v-model="addProdutForm.name" placeholder="请输入产品名称进行添加" style="width:224px"></el-input>
                    <el-button plain size="mini" style="margin-left:12px" @click="resetForm('addProdutForm')">取消</el-button>
                    <el-button plain size="mini" @click="submitForm('addProdutForm')" :disabled='loading'>确定</el-button>
                </div>
            </el-form-item>
        </el-form>
        <div class="add_pro_btn" v-else>
            <el-button plain icon="el-icon-circle-plus-outline" style="width:100%" @click="show=true">添加产品</el-button>
        </div>
        <ul class="product_list">
            <li class="product_item" v-for="(item,index) in productList" :key="index">{{item.name}}</li>
        </ul>
        <span slot="footer" class="dialog-footer">
            <el-button size="small" @click="closeForm()" class="el-button-width">取 消</el-button>
            <el-button size="small" type="primary" @click="closeForm()" class="el-button-width">确 定</el-button>
        </span>
    </el-dialog>
</template>

<script>
export default {
    name: "",
    data() {
        var errorValue = (rule, value, callback) => {
            let reg = /[^\a-\z\A-\Z0-9\u4E00-\u9FA5]/;
            if (reg.test(value)) {
                return callback(new Error("不能包含特殊字符！"));
            } else {
                callback();
            }
        };
        return {
            loading: false,
            dialogVisible: false,
            show: false,
            productList: [],
            queryInfo: {
                bid: "",
                page: 1,
                limit: 0,
            },
            addProdutForm: {
                bid: "",
                name: "",
            },
            addProdutRules: {
                name: [
                    {
                        required: true,
                        message: "请输入产品名称",
                        trigger: "blur",
                    },
                    { validator: errorValue, trigger: ["blur", "change"] },
                ],
            },
        };
    },
    created() {
        this.$bus.on("addProdut", (value) => {
            this.dialogVisible = value.dialogVsb;
            this.queryInfo.bid = value.bid;
            this.addProdutForm.bid = value.bid;
            this.getProductList();
        });
    },
    beforeDestroy() {
        this.$bus.off("addProdut");
    },
    methods: {
        //获取列表
        async getProductList() {
            const { data: res } = await this.$http.post(
                "adminapi/brand/productSelect",
                this.queryInfo
            );
            if (res.code !== 200) {
                this.$message.error(res.msg);
                return;
            } else {
                this.productList = res.data.list;
            }
        },

        // 提交添加
        submitForm(formName) {
            this.$refs[formName].validate(async (valid) => {
                if (valid) {
                    this.loading = true;
                    const { data: res } = await this.$http.post(
                        "adminapi/brand/productAdd",
                        this.addProdutForm
                    );
                    if (res.code !== 200) {
                        this.$message.error(res.msg);
                        this.loading = false;
                        return;
                    } else {
                        this.$message({
                            message: "添加成功",
                            type: "success",
                        });
                        this.loading = false;
                        this.resetForm(formName);
                        this.getProductList();
                    }
                } else {
                    this.loading = false;
                    return false;
                }
            });
        },

        // 关闭重置表单
        resetForm() {
            this.show = false;
        },

        closeForm() {
            this.dialogVisible = false;
            this.show = false;
            this.addProdutForm.name = "";
            this.$parent.getDataList(1);
        },
    },
    components: {},
};
</script>


<style scoped>
.add_pro_btn {
    margin-bottom: 22px;
}
.product_list {
    margin: -2px -20px 0;
    overflow-y: auto;
    max-height: 288px;
}
.product_item {
    height: 48px;
    line-height: 48px;
    font-size: 14px;
    color: #17233d;
    padding: 0 20px;
}
.product_item:hover {
    background-color: #f6f8ff;
}
</style>
